package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Email;

import java.util.List;

/**
 * Created by Student on 21.11.2014.
 */
public class Queue {
    private List<Email> emailList;
    private Object addObj = new Object();
    private Object remObj = new Object();

    public Queue(List<Email> emailList) {
        this.emailList = emailList;
    }

    public void add(Email email) {
        synchronized (addObj) {
            emailList.add(email);
        }
        synchronized (this) {
            notify();
        }
    }


    public Email remove() throws InterruptedException {
        Email email = null;

        synchronized (addObj){

            if (!emailList.isEmpty()) {
                email = emailList.remove(0);
            }
        }

        synchronized (this) {
            if (email == null) {
                wait();
            }

        }

        return email;
    }

}
